//app.js
const api = require('./utils/config');
const utils = require('./utils/util');
App({
  data: {
    api: api,
    utils: utils,
    userInfo: {},
  },
  onLaunch: function () {
    console.log(wx.cloud);
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    wx.showModal({
      title: '订阅服务消息开启提示',
      content: '订阅消息开启之后，才能接收到健康上报提醒哦,如已开启，请忽略！',
      success (res) {
        if (res.confirm) {
          wx.requestSubscribeMessage({
            tmplIds: ['1TuTNhSDR8cmshs7X3w7SvWx7FKHg3pHjl4xJmUK1Js', 'slJT88J2vrT2JNJ9ruZkcINuQPJAyUIGxojbwrBW5rE' ],
            success: (res)=> {
              console.log(res);
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    
    
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        var code = res.code;
        utils.request({
          url: api.login,
          data: {code: code},
          method: 'post',
          success: (data) => {
            wx.setStorageSync('userInfo', data);
            console.log(wx.getStorageSync('userInfo'));
            if(data.phone == null){
              wx.showModal({
                title: '完善信息提示',
                content: '您还没有完善信息，快去完善信息吧！',
                success (res) {
                  if (res.confirm) {
                    wx.redirectTo({
                      url: '/pages/information/index',
                    })
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                  }
                }
              })
              
            }
          }
        })

      }
    })
    if (wx.cloud) {
      wx.cloud.init({
        traceUser: true
      })
    }
  },
})